#   -*- mode: autoconf; coding: utf-8; -*-
#
#   Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
#   This file is part of Intromit.
#
#   Intromit is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   Intromit is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with Intromit.  If not, see <http://www.gnu.org/licenses/>.

dnl Process this file with autoconf to produce a configure script.

AC_INIT(intromit, VERSION, asier.aguirre@gmail.com)
AC_CONFIG_AUX_DIR(config)

AM_INIT_AUTOMAKE(intromit, `cat VERSION`)
AM_CONFIG_HEADER(src/config.h)

dnl Package
STR_INTROMIT="intromit-`cat VERSION`"
AC_SUBST(STR_INTROMIT)

dnl Set prefix
AC_PREFIX_DEFAULT(/usr/local/intromit)
test "$prefix" = NONE && prefix=/usr/local/intromit

dnl Check for required tools
AC_PROG_CC
AM_PROG_CC_STDC
AC_PROG_CPP
AC_PROG_INSTALL
AM_PROG_LIBTOOL
AC_PROG_CXX
AC_HEADER_STDC

dnl User specific options
AC_ARG_WITH([included-libdw],
	[AS_HELP_STRING([--with-included-libdw],
		[use package included libdw @<:@default=no@:>@;
		note that those libraries will not get installed in the system])],
	[],
	[with_included_libdw=no])

AC_ENABLE_DEBUG(yes)
case $enable_debug in
yes) DEBUG_OPTS="-g -DDEBUG_CC" ;;
no)  DEBUG_OPTS="" ;;
*)   DEBUG_OPTS="" ;;
esac

AC_ENABLE_OPTIM(no)
case $enable_optim in
yes) OPTIM_OPTS="-O2 -fexpensive-optimizations" ;;
no)  OPTIM_OPTS="-O0" ;;
*)   OPTIM_OPTS="-O0" ;;
esac

AC_ENABLE_PROFILE(no)
case $enable_profile in
yes) PROFILE_OPTS="-pg -fprofile-arcs" ;;
no)  PROFILE_OPTS="" ;;
*)   PROFILE_OPTS="" ;;
esac

dnl Set CFLAGS, CXXFLAGS and LDFLAGS
CFLAGS="$GCC_OPTS $CC_OPTS -Wall -DPREFIX=\"$prefix\" $DEBUG_OPTS $OPTIM_OPTS $PROFILE_OPTS"
CXXFLAGS="$CXXFLAGS $CFLAGS"
LDFLAGS="$PROFILE_OPTS"

dnl Check for required libraries and programs and show all at a time
STR="Posix thread library is required. Package aliases:\n"
STR=$STR"\tDebian   libc6-dev\n"
LMISS_PTHREAD=$STR"\n"

STR="Readline library is required. Package aliases:\n"
STR=$STR"\tDebian   libreadline5-dev [OLD: libreadline4-dev]\n"
STR=$STR"\tCentOS   libreadline-devel [CHECK: libreadline.so and libtermcap.so are accessible]\n"
LMISS_READLINE=$STR"\n"

STR="ELF object file access library required. If you system is too\n"
STR=$STR"old, try --with-included-libdw. Package aliases:\n"
STR=$STR"\tDebian   libelf-dev libelf1 [OLD: libelfg0-dev, libelfg0]\n"
STR=$STR"\tCentOS   elfutils-libelf-devel\n"
LMISS_ELF=$STR"\n"

STR="DWARF access library is required. Old distributions do not provide this package,\n"
STR=$STR"and probably, provided elfutils is obsolete.\n"
STR=$STR"For convenience, this part of elfutils is included. To use it, try\n"
STR=$STR"configure option --with-included-libdw. Package aliases:\n"
STR=$STR"\tDebian   libdw-dev\n"
STR=$STR"\tCentOS   elfutils-devel [CHECK: libdw.so (with no version) is accessible]\n"
STR=$STR"\tSource   http://ftp.debian.org/debian/pool/main/e/elfutils/elfutils_X.XXX.orig.tar.gz\n"
LMISS_DW=$STR"\n"

STR="Development DWARF access library is required. Package aliases:\n"
STR=$STR"\tDebian   libdw-dev\n"
STR=$STR"\tCentOS   elfutils-devel [CHECK: libdw.so (with no version) is accessible]\n"
STR=$STR"\tSource   http://ftp.debian.org/debian/pool/main/e/elfutils/elfutils_X.XXX.orig.tar.gz\n"
LMISS_DW_INCLUDE=$STR"\n"

STR="BFD development library is required. If package is already installed and this message\n"
STR=$STR"keeps on appearing, a very old library is being used and a newer is required\n"
STR=$STR"Package aliases:\n"
STR=$STR"\tDebian   binutils-dev\n"
STR=$STR"\tCentOS   binutils-devel\n"
LMISS_BINUTILS=$STR"\n"

STR="Libiberty library is required. Package aliases:\n"
STR=$STR"\tDebian   binutils-dev\n"
STR=$STR"\tCentOS   binutils-devel\n"
LMISS_LIBERTY=$STR"\n"

STR="Math library is required. Package aliases:\n"
STR=$STR"\tDebian   libc6-dev\n"
LMISS_MATH=$STR"\n"

STR="Python interpreter and development files are required. Package aliases:\n"
STR=$STR"\tDebian   python, python-dev\n"
STR=$STR"\tCentOS   python, python-devel\n"
LMISS_PYTHON=$STR"\n"

STR="Cut tool is required to extract python version.\n"
STR=$STR"Use --with-python to overryde. Package aliases:\n"
STR=$STR"\tDebian   coreutils\n"
LMISS_COREUTILS=$STR"\n"

STR="Make tool is required to build the package. Package aliases:\n"
STR=$STR"\tDebian   make\n"
LMISS_MAKE=$STR"\n"

STR="A C++ compiler is required. Package aliases:\n"
STR=$STR"\tDebian   g++ or g++-V.V\n"
STR=$STR"\tCentOS   gcc-c++\n"
LMISS_CXX=$STR"\n"

STR="Development FreeType 2 font engine required:\n"
STR=$STR"\tDebian   libfreetype6-dev\n"
LMISS_FREETYPE=$STR"\n"


dnl Check system libelf and libdw
AC_CHECK_LIB(elf,      elf_begin,      [echo -n], [LMISS=$LMISS$LMISS_ELF])
AC_CHECK_LIB(dw,       dwarf_begin,    [echo -n], [LMISS=$LMISS$LMISS_DW], -lelf)

if test "$with_included_libdw" = "yes"; then

   dnl Remove previous errors
   LMISS=""
   dnl Uncompress dep
   cd dep && tar xfz dep.tar.gz && cd -
   dnl Create define: to use includes in the code
   AC_DEFINE_UNQUOTED([INCLUDED_LIBDW], ["$(pwd)/dep/include/"], [Use package included libdw])
   dnl libdw LDFLAGS / INCLUDE
   LIBDW_LDFLAGS="$(pwd)/dep/lib/libdw.a $(pwd)/dep/lib/libelf.a"
   LIBDW_INCLUDE="$(pwd)/dep/include/libdw.h"

else

   dnl libdw LDFLAGS / INCLUDE
   LIBDW_LDFLAGS="-ldw -lelf"
   LIBDW_INCLUDE="elfutils/libdw.h"

fi

AC_SUBST(LIBDW_LDFLAGS)
AC_CHECK_HEADERS([$LIBDW_INCLUDE],     [echo -n], [LMISS=$LMISS$LMISS_DW_INCLUDE],
[
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
])

AC_CHECK_LIB(pthread,  pthread_create, [echo -n], [LMISS=$LMISS$LMISS_PTHREAD])
AC_CHECK_LIB(termcap,  tgetstr)
AC_CHECK_LIB(readline, readline,       [echo -n], [LMISS=$LMISS$LMISS_READLINE])
AC_CHECK_LIB(bfd,      bfd_openr,      [echo -n], [LMISS=$LMISS$LMISS_BINUTILS], -liberty)
AC_CHECK_LIB(m,        cos,            [echo -n], [LMISS=$LMISS$LMISS_MATH])
AC_CHECK_LIB(freetype, FT_New_Face,    [echo -n], [LMISS=$LMISS$LMISS_FREETYPE])
AC_CHECK_PROG(make,    make,           "yes",     "no")
if test "$make" = "no"; then
   LMISS=$LMISS$LMISS_MAKE
fi
AC_CHECK_PROG(cut,     cut,            "yes",     "no")

dnl Check for a C++ compiler
AC_CHECK_PROG(have_cxx, [$(basename $CXX)], "yes", "no")
if test "$have_cxx" = "no"; then
   LMISS=$LMISS$LMISS_CXX
fi

dnl Only newer binutils packages implement this function
AC_CHECK_LIB(bfd,      bfd_demangle,
	[AC_DEFINE([HAVE_DEMANGLE], [1], [BFD demangle function.])],
	[AC_CHECK_LIB(iberty, cplus_demangle, , [LMISS=$LMISS$LMISS_LIBERTY])])

dnl Python specific
dnl Select python version
AC_ARG_WITH([python],
	[AS_HELP_STRING([--with-python=VERSION],
		[select python version @<:@default=check@:>@])],
	[],
	[with_python=check])

if test "$with_python" = "check"; then
   dnl Automatically determine python flags
   AC_CHECK_PROG(pyconfig, python-config, "yes",  "no")
   if test "$pyconfig" = "yes"; then
      dnl Use python-config
      PYTHON_INCLUDES=`python-config --includes`
      PYTHON_LDADD=`python-config --libs`
   else
      dnl Determine python version using python -V
      AC_CHECK_PROG(python, python,    "yes",     "no")
      if test "$python" = "no"; then
      	 LMISS=$LMISS$LMISS_PYTHON
      elif test "$cut" = "no"; then
      	 LMISS=$LMISS$LMISS_COREUTILS
      else
	 python=$(( python -V 2>&1 > /dev/null ) | cut -f2 -d' ' | cut -f1,2 -d'.')
	 AC_CHECK_PROG(pyconfigv, python$python-config, "yes",  "no")
	 if test "$pyconfigv" = "yes"; then
	    dnl Use python-config
	    PYTHON_INCLUDES=`python$python-config --includes`
	    PYTHON_LDADD=`python$python-config --libs`
	 else
	    AC_CHECK_LIB(python$python, Py_Main, [echo -n],
		[LMISS=$LMISS"python$python-dev missing\n"$LMISS_PYTHON])
	    PYTHON_INCLUDES=-I/usr/include/python$python
	    PYTHON_LDADD=-lpython$python
	    AC_CHECK_HEADERS([python$python/Python.h], [echo -n], [LMISS=$LMISS$LMISS_PYTHON])
	 fi
      fi
   fi
else
   dnl Python version supplied by user
   AC_CHECK_PROG(pyconfig,python$with_python-config, "yes", "no")
   if test "$pyconfig" = "yes"; then
      dnl Use python-config
      PYTHON_INCLUDES=`python$with_python-config --includes`
      PYTHON_LDADD=`python$with_python-config --libs`
   else
      dnl Check the version provided directly
      AC_CHECK_LIB(python$with_python, Py_Main, [echo -n],
		[LMISS=$LMISS"python$with_python-dev required\n"$LMISS_PYTHON])
      PYTHON_INCLUDES=-I/usr/include/python$with_python
      PYTHON_LDADD=-lpython$with_python
      AC_CHECK_HEADERS([python$with_python/Python.h], [echo -n], [LMISS=$LMISS$LMISS_PYTHON])
   fi
fi
AC_SUBST(PYTHON_INCLUDES)
AC_SUBST(PYTHON_LDADD)

dnl Prompt all missing libraries/programs at a time
if test "$LMISS"; then echo -en "\n"$LMISS; AC_MSG_ERROR([install required packages]); fi

dnl Doxygen and project documentation
AC_CHECK_PROG(exist_doxygen,   doxygen,   "yes", "no")
AC_CHECK_PROG(exist_dot,       dot,       "yes", "no")
AC_CHECK_PROG(exist_latex,     latex,     "yes", "no")
AC_CHECK_PROG(exist_makeindex, makeindex, "yes", "no")
AC_CHECK_PROG(exist_dvips,     dvips,     "yes", "no")
AC_CHECK_PROG(exist_ps2pdf,    ps2pdf,    "yes", "no")
AM_CONDITIONAL(EXIST_DOXYGEN,  test "$exist_doxygen" = "yes")
AM_CONDITIONAL(EXIST_LATEX,    test "$exist_latex"   = "yes" -a "$exist_makeindex" = "yes" -a "$exist_dvips" = "yes" -a "$exist_ps2pdf" = "yes")
AC_SUBST(EXIST_DOT, "$exist_dot")

dnl Output files...
AC_OUTPUT(
	Makefile \
	etc/Makefile \
	src/Makefile \
	src/util/Makefile \
	src/intromit/Makefile \
	src/backends/Makefile \
	src/backends/python/Makefile \
	src/backends/python_embed/Makefile \
	src/generators/Makefile \
	src/generators/gendata/Makefile \
	src/frameworks/Makefile \
	src/frameworks/introsight/Makefile \
	test/Makefile \
	test/python_config.py \
	doc/Makefile \
	doc/doxygen.cfg)

dnl Obsolete tools warnings
if test "$cut" = "yes"; then

   dnl PYTHON
   if (( $(( python -V 2>&1 > /dev/null ) | cut -f2 -d' ' | cut -f2 -d'.') < 4 )); then
      echo "Warning: There are some python problems with python versions < 2.4"
   fi

   dnl GCC
   if (( $(( g++ -v 2>&1 > /dev/null ) | tail -1 | cut -f3 -d' ' | cut -f1 -d'.') < 3 )); then
      dnl gcc version 2
      echo "Warning: GCC version 2 not supported"
   elif (( $(( g++ -v 2>&1 > /dev/null ) | tail -1 | cut -f3 -d' ' | cut -f1 -d'.') < 4 )); then
      dnl gcc version 3
      if (( $(( g++ -v 2>&1 > /dev/null ) | tail -1 | cut -f3 -d' ' | cut -f2 -d'.') < 4 )); then
      	 dnl gcc 3.3 or older
	 echo "Warning: GCC version 3.3 and older ones have several problems with DWARF"
      fi
   fi
fi
